package com.yc.sole.erp.controller;

import com.yc.sole.erp.biz.EmployeeBiz;
import com.yc.sole.erp.request.EmployeeQueryReq;
import com.yc.sole.erp.request.EmployeeReq;
import com.yc.sole.erp.response.EmployeeResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 员工管理
 *
 * @author yizuomin
 * @date 2024-01-04
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeBiz employeeBiz;

    /**
     * 新增
     *
     * @param employeeReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:employee:save", description = "新增职员")
    @PostMapping("/api/base-data/employee/save")
    public Result<Void> saveEmployee(@RequestBody @Validated(SaveGroup.class) EmployeeReq employeeReq) {
        employeeBiz.saveEmployee(employeeReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param employeeReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:employee:update", description = "修改职员")
    @PostMapping("/api/base-data/employee/update")
    public Result<Void> updateEmployeeById(@RequestBody @Validated(UpdateGroup.class) EmployeeReq employeeReq) {
        employeeBiz.updateEmployeeById(employeeReq);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "base-data:employee:delete", description = "删除职员")
    @PostMapping("/api/base-data/employee/delete")
    public Result<Void> deleteEmployee(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        employeeBiz.deleteEmployee(baseDeleteReq.getIds());
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "base-data:employee:select", description = "查询职员")
    @PostMapping("/api/base-data/employee/page")
    public Result<Paging<EmployeeResp>> selectEmployeePage(@RequestBody @Validated BasePageReq<EmployeeQueryReq> basePageReq) {
        return Result.data(employeeBiz.selectEmployeePage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 根据职员名称查询
     *
     * @param employeeQueryReq
     * @return Result
     */
    @PreAuth(value = "base-data:employee:select", description = "根据职员名称查询")
    @PostMapping("/api/base-data/employee/list")
    public Result<List<EmployeeResp>> selectEmployeeByName(@RequestBody @Validated EmployeeQueryReq employeeQueryReq) {
        return Result.data(employeeBiz.selectEmployeeByName(employeeQueryReq));
    }

    /**
     * 根据id查询员工
     *
     * @param employeeId
     * @return Result
     */
    @PreAuth(value = "base-data:employee:select", description = "根据id查询员工")
    @PostMapping("/api/base-data/employee/{employeeId}")
    public Result<EmployeeResp> selectEmployeeById(@PathVariable Long employeeId) {
        return Result.data(employeeBiz.selectEmployeeById(employeeId));
    }
}
